package org.tlipoca.smchannel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.genntii.smcommon.constant.ExceptionConstant;
import org.genntii.smcommon.exception.ChannelNotFoundException;
import org.genntii.smcommon.exception.SteamAppNotFoundException;
import org.genntii.smcommon.result.PageResult;
import org.genntii.smdomain.entity.Channel;
import org.genntii.smdomain.vo.ChannelVO;
import org.springframework.stereotype.Service;
import org.tlipoca.smchannel.client.SteamStoreClient;
import org.tlipoca.smchannel.client.UserSubscribeClient;
import org.tlipoca.smchannel.mapper.ChannelMapper;
import org.tlipoca.smchannel.service.ChannelService;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, Channel> implements ChannelService {

    @Resource
    private ChannelMapper channelMapper;

    @Resource
    private SteamStoreClient steamStoreClient;

    @Resource
    private UserSubscribeClient userSubscribeClient;

    @Override
    public PageResult search(String keyword, int pageNum, int pageSize) {
        ArrayList<Channel> channels = channelMapper.search(keyword,(pageNum - 1) * pageSize, pageSize);
        System.out.println(channels);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        ArrayList<ChannelVO> channelVOS = new ArrayList<>();
        for (Channel channel:channels){
            channelVOS.add(ChannelVO.builder()
                    .id(channel.getId())
                    .channelDescription(channel.getChannelDescription())
                    .channelIcon(channel.getChannelIcon())
                    .updateTime(dateTimeFormatter.format(channel.getUpdateTime()))
                    .channelName(channel.getChannelName())
                    .status(channel.getStatus())
                    .appid(channel.getAppid())
                    .build());
        }
        return PageResult.build(channelVOS,channelMapper.searchTotal(keyword));
    }

    @Override
    public ArrayList<ChannelVO> getChannelVOList() {
        ArrayList<Channel> channels = channelMapper.getChannelList();
//        ArrayList<Long> channelIds = new ArrayList<>();
//        for (Channel channel:channels){
//            channelIds.add(channel.getId());
//        }
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        ArrayList<ChannelVO> channelVOS = new ArrayList<>();
        for (Channel channel:channels){
            channelVOS.add(ChannelVO.builder()
                    .id(channel.getId())
                    .channelDescription(channel.getChannelDescription())
                    .channelIcon(channel.getChannelIcon())
                    .updateTime(dateTimeFormatter.format(channel.getUpdateTime()))
                    .channelName(channel.getChannelName())
                    .status(channel.getStatus())
                    .appid(channel.getAppid())
                    .build());
        }
        return channelVOS;
    }

    @Override
    public Boolean channelExistsService(Long channelId) {
        return channelMapper.channelExists(channelId) == 1;
    }

    @Override
    public Channel getChannelByAppIdService(Integer appid, Long userId) {
        // 判断appid是否存在
        if(!steamStoreClient.steamStoreExists(appid).getData()) {
            throw new SteamAppNotFoundException(ExceptionConstant.STEAM_APP_NOT_FOUND_EXCEPTION);
        }

        Channel channel = channelMapper.selectChannelByAppId(appid);
        channel.setSubscribed(userSubscribeClient.userSubscribed((byte) 1,channel.getId(),userId).getData());
        return channel;
    }

    @Override
    public ChannelVO getChannelByIdService(Long channelId, Long userId) {
        Channel channel = channelMapper.selectById(channelId);

        if(channel == null) {
            throw new ChannelNotFoundException(ExceptionConstant.CHANNEL_NOT_FOUND_EXCEPTION);
        }
        channel.setSubscribed(userSubscribeClient.userSubscribed((byte) 1,channel.getId(),userId).getData());
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return ChannelVO.builder()
                .id(channel.getId())
                .channelName(channel.getChannelName())
                .appid(channel.getAppid())
                .channelDescription(channel.getChannelDescription())
                .channelIcon(channel.getChannelIcon())
                .status(channel.getStatus())
                .updateTime(channel.getUpdateTime().format(dateTimeFormatter))
                .subscribed(userSubscribeClient.userSubscribed((byte) 1,channel.getId(),userId).getData())
                .build();

    }

    @Override
    public Channel getChannelById(Long channelId, Long userId) {
        Channel channel = channelMapper.selectById(channelId);

        if(channel == null) {
            throw new ChannelNotFoundException(ExceptionConstant.CHANNEL_NOT_FOUND_EXCEPTION);
        }
        channel.setSubscribed(userSubscribeClient.userSubscribed((byte) 1,channel.getId(),userId).getData());
        return channel;
    }

    @Override
    public Map<Long, ChannelVO> getChannelListByIdsService(ArrayList<Long> channelIds, Long userId) {

        List<Channel> channelList = channelMapper.selectList(new QueryWrapper<Channel>().in("id", channelIds));
        Map<Long, ChannelVO> channelMap = new HashMap<>();

        for (Channel channel : channelList) {
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            channelMap.put(channel.getId(), ChannelVO.builder()
                    .id(channel.getId())
                    .channelName(channel.getChannelName())
                    .appid(channel.getAppid())
                    .channelDescription(channel.getChannelDescription())
                    .channelIcon(channel.getChannelIcon())
                    .status(channel.getStatus())
                    .updateTime(channel.getUpdateTime().format(dateTimeFormatter))
                    .subscribed(userSubscribeClient.userSubscribed((byte) 1,channel.getId(),userId).getData())
                    .build());
        }

        return channelMap;
    }

    @Override
    public ArrayList<Channel> getChannelBatch(ArrayList<Long> channelIds) {
        if (channelIds.isEmpty()){
            return null;
        }
        ArrayList<Channel> channelBatch = channelMapper.getChannelBatch(channelIds);
        for (Channel channel:channelBatch){
            channel.setUpdateTime(null);
        }
        return channelBatch;
    }
}
